package com.lemo.demo.hadoop.model;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;

public class WirtePair implements WritableComparable<WirtePair>{
	
	private String first;
	private int second;
	
	public WirtePair() {
		
	}

	public WirtePair(String first, int second) {
		this.first = first;
		this.second = second;
	}
	
	
	public String getFirst() {
		return first;
	}

	public void setFirst(String first) {
		this.first = first;
	}

	public int getSecond() {
		return second;
	}

	public void setSecond(int second) {
		this.second = second;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((first == null) ? 0 : first.hashCode());
		result = prime * result + second;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		WirtePair other = (WirtePair) obj;
		if (first == null) {
			if (other.first != null)
				return false;
		} else if (!first.equals(other.first))
			return false;
		if (second != other.second)
			return false;
		return true;
	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.writeUTF(this.first);
		out.writeInt(this.second);
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		this.first = in.readUTF();
		this.second = in.readInt();
	}

	@Override
	public int compareTo(WirtePair o) {
		int comp = this.first.compareTo(o.getFirst());
		if(comp == 0) {
			return Integer.valueOf(this.second).compareTo(Integer.valueOf(o.getSecond()));
		}
		return comp;
	}

	@Override
	public String toString() {
		return "[first=" + first + ", second=" + second + "]";
	}

}
